package bungaeting.net.client.android.activity;

import java.util.ArrayList;

import bungaeting.net.client.android.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class groupManage extends Activity {	
	private static final String KEY_PREF = "group";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.groupmanage);

        Cursor cursor = getURI();					// ��ȭ��ȣ�� ��������	

		int end = cursor.getCount();				// ��ȭ��ȣ���� ���� ����

		final ArrayList<String> name = new ArrayList<String>();			// ��ȭ��ȣ���� �̸��� ������ �迭 ����
		final ArrayList<String> nameRight = new ArrayList<String>();
		int count = 0;	
		
		if(cursor.moveToFirst()) 
		{
			
			int idIndex = cursor.getColumnIndex("_id");	// �÷������� �÷� �ε��� ã�� 

			do 
			{
				//db.execSQL("INSERT INTO product VALUES (null, '"+ cursor.getString(1) +"', 900);");
				int id = cursor.getInt(idIndex);	// ��Ұ� ���		
				name.add(cursor.getString(1));

				count++;
			} while(cursor.moveToNext() || count > end);
		}
		
//		  ClientDb mHelper = new ClientDb(this);
//        SQLiteDatabase db = mHelper.getWritableDatabase();
        
//        cursor = db.rawQuery("SELECT * FROM product", null);
//        startManagingCursor(cursor);
//        
//        SimpleCursorAdapter Adapter = null;
//        
//        Adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, new String[] {"name"} ,
//        new int[] { R.id.label });
        
//        ListView list = (ListView)findViewById(R.id.list);
//        list.setAdapter(Adapter);  
        
		final ArrayAdapter<String> leftAdapter = new ArrayAdapter<String>(this, R.layout.row, R.id.label, name);
		final ArrayAdapter<String> rightAdapter = new ArrayAdapter<String>(this, R.layout.row, R.id.label, nameRight);
		
        ListView left = (ListView)findViewById(R.id.left_list);
        left.setAdapter(leftAdapter);
        left.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        
        ListView right = (ListView)findViewById(R.id.right_list);
        right.setAdapter(rightAdapter);
        right.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        
	    left.setOnItemClickListener(new ListView.OnItemClickListener() {
	    	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	    		nameRight.add(leftAdapter.getItem(position).toString());
	    		name.remove(position);
	    		rightAdapter.notifyDataSetChanged();
	    		leftAdapter.notifyDataSetChanged();
	    	}
	    });
	    
	    right.setOnItemClickListener(new ListView.OnItemClickListener() {
	    	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	    		name.add(rightAdapter.getItem(position).toString());
	    		nameRight.remove(position);
	    		rightAdapter.notifyDataSetChanged();
	    		leftAdapter.notifyDataSetChanged();
	    	}
	    });

    }
    
    public Cursor getURI(){
		// �ּҷ� URI		
		Uri people = Contacts.People.CONTENT_URI;
		
		// �˻��� �÷� ���ϱ�
		String[] projection = new String[] { Contacts.People._ID, Contacts.People.DISPLAY_NAME, Contacts.People.NUMBER };
		
		// ���� ������ Ŀ�� ���
		String[] selectionArgs = null;
		String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";	

		return managedQuery(people, projection, null, selectionArgs, sortOrder);
	}
    
    public void groupClick(View v) {
        Intent map = new Intent(this, group.class);
        startActivity(map);
    }
    
    public void mapClick(View v) {
        Intent map = new Intent(this, googleMap.class);
        startActivity(map);
    }
}